package com.evildog.workspace.life.application.task;

import com.evildog.workspace.life.application.bo.CalendarDayBO;
import com.evildog.workspace.life.application.config.WorkspaceApiProperties;
import com.evildog.workspace.life.application.consts.RedisKeyConst;
import com.evildog.workspace.life.application.forest.CalendarApi;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@AllArgsConstructor
@Slf4j
public class CalendarTask {

    private final RedissonClient redissonClient;
    private final CalendarApi calendarApi;
    private final WorkspaceApiProperties workspaceApiProperties;

    /**
     * 每天，生成当年的假期提醒数据
     */
    @Scheduled(cron = "0 0 0 0 1 ? *")
    public void queryCalendarDay() {
        RLock lock = redissonClient.getLock(RedisKeyConst.WEATHER_LOCK);
        try {
            if(lock.tryLock()){
                LocalDate now = LocalDate.now();
                CalendarDayBO calendarDay = calendarApi.getCalendarDay(now.toString(), workspaceApiProperties.getJuhe());

            }
        } catch (Exception e) {
            log.error("queryCalendarDay Error occurred",e);
        } finally {
            lock.unlock();
        }
    }
}
